package com.spring.service.impl;

import com.spring.pojo.User;
import com.spring.respone.ApiRestResponse;
import com.spring.service.RegisterService;
import com.spring.service.dao.UserDao;
import com.spring.until.PassWordsUtil;
import com.spring.until.SessionUtils;
import com.spring.until.UserPass;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

@Service
public class RegisterServiceImpl implements RegisterService {

    @Autowired
    UserDao userDao ;

    @Override
    public ApiRestResponse register(HttpServletRequest request) {
        String password = request.getParameter("password") ;
        String userPass = request.getParameter("userPass") ;
        boolean kind = Boolean.getBoolean(request.getParameter("kind")) ;
        String code = request.getParameter("code") ;
        String name = request.getParameter("name") ;
        String id = request.getParameter("id") ;
        String stdId = request.getParameter("stdId") ;
        User user = new User() ;
        if(kind){
            user.setPhone(userPass);
        }else{
            user.setEmail(userPass);
        }
        user.setName(name);
        password = PassWordsUtil.toMd5(password) ;
        user.setPassword(password);
        user.setStdId(stdId);
        int ok = userDao.insert(user) ;
        if(ok==1){
            return ApiRestResponse.success().setStatus(1).setMsg("注册成功") ;
        }else{
            return ApiRestResponse.error().setStatus(1).setMsg("注册失败");
        }
    }

    @Override
    public ApiRestResponse forgotPwd(HttpServletRequest request) {
        boolean kind = Boolean.parseBoolean(request.getParameter("kind"));
        String userPass = request.getParameter("userPass");
        String password = request.getParameter("password");
        User user ;
        boolean flag = true ;
        if(kind){
            user = userDao.selectByPhone(userPass) ;
        }else{
            user = userDao.selectByEmail(userPass) ;
        }
        if(user!=null){
            user.setPassword(PassWordsUtil.toMd5(password));
            int ans =  userDao.update(user)  ;
            return ApiRestResponse.success().setMsg("登录成功").setStatus(1) ;
        }else{
            return ApiRestResponse.error().setMsg("登录失败").setStatus(0);
        }
    }

    @Override
    public ApiRestResponse checkUserPass(HttpServletRequest request) {
        String userPass = request.getParameter("userPass") ;
        boolean kind = Boolean.parseBoolean(request.getParameter("kind")) ;
        User user ;
        if(kind){
            user = userDao.selectByPhone(userPass) ;
        }else{
            user = userDao.selectByEmail(userPass) ;
        }
        if(user==null){
            return ApiRestResponse.success().setStatus(1).setMsg("可用") ;
        }
        return ApiRestResponse.error().setStatus(0).setMsg("有重复用户");
    }

    @Override
    public ApiRestResponse checkCode(HttpServletRequest request) {
        String code = request.getParameter("code");
        boolean kind = Boolean.parseBoolean(request.getParameter("kind")) ;
        String pass  = request.getParameter("userPass") ;
        UserPass userPass = new UserPass(kind,pass) ;
        if(code.equals(SessionUtils.getCode(userPass))){
            return ApiRestResponse.success().setStatus(1).setMsg("验证码正确") ;
        }
        return ApiRestResponse.error().setStatus(0).setMsg("验证码错误");
    }
}
